package 蓝桥杯算法;

public class day35 {
    public static void main(String[] args) {
        int[][] arr = {{1, 2}, {1, 3}, {2, 4}, {3, 4}};
        //并集初始化
        int[] father = new int[4];
    }
}
class DisJonit{
    public static void main(String[] args) {
        DisJonit a=new DisJonit();
        //添加路径
        int[][]arr={{1,2},{1,3},{2,4},{3,4}};
        a.DisJonit1(4);
        for(int i=0;i<arr.length;i++){
            a.join(arr[i][0],arr[i][1]);
        }
        System.out.println( a.issame(1,4));
    }
    int[] fathet;
    public void DisJonit1(int N){
        fathet=new int[N];
        for(int i=0;i<N;i++){
            fathet[i]=i;
        }
    }
    public int find(int n){
        if(n==fathet[n]){
            return n;
        }
         return fathet[n]=find(n);
    }
    public void join(int n,int m){
        n=find(n);
        m=find(m);
        if(n==m){
            return;
        }
        fathet[m]=n;
    }
    public boolean issame(int n,int m){
        n=find(n);
        m=find(m);
        return n==m;
    }
}
